/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.AreaDAO;
import com.cashmate.hibernate.dao.BorrowerDAO;
import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Borrowers;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Loanverification;
import com.opensymphony.xwork2.ActionSupport;
import java.io.InputStream;
import java.io.StringBufferInputStream;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */
@Action(value = "/get-binfoloanapp", results = {
    @Result(name = "success", type = "stream", params = {"inputName", "inputStream", "contentType", "text/html"}),
    @Result(name = "error", location = "WEB-INF/error/indexError.html")
})
public class GetBorrowerInfoLoanApp extends ActionSupport {

    private static Logger logger = Logger.getLogger(GetBorrowerName.class);
    private BorrowerDAO bo = (BorrowerDAO) ServletActionContext.getServletContext().getAttribute("borrowerDAO");
    private AreaDAO ao = (AreaDAO) ServletActionContext.getServletContext().getAttribute("areaDAO");
    private LoansDAO loanDAO = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    public String firstname, middlename, lastname, bcode;
    private InputStream inputStream;
    private Borrowers bor;

    public void setBcode(String bcode) {
        this.bcode = bcode;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setMiddlename(String middlename) {
        this.middlename = middlename;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    @Override
    public String execute() {
//        System.out.println("HELLO PO JEJEJE");
        List<Borrowers> allbors = null;
        boolean hasMatch = false;
        try {
            if (!bcode.isEmpty()) {
                allbors = bo.getAll();
                for (int i = 0; i < allbors.size(); i++) {
                    bor = allbors.get(i);
                    String borcode = allbors.get(i).getBorrowercode();
                    if (borcode.equals(bcode)) {
                        String borrowercode = "code:" + bor.getBorrowercode() + "," + bor.getBorrowerid() +"|";
                        String name = "name:" + bor.getFirstname() + "," + bor.getMiddlename() + "," + bor.getLastname() + "|";
                        String address = "address:" + bor.getStreetaddress() + "," + bor.getBarangay() + "," + bor.getCityormunicipality() + "," + bor.getProvince() + "|";
                        String areacode = "area:" + bor.getAreacode() + "|";
                        Loans latestloan = null;
                        Loanverification latestloanver = null;
                        try {
                            latestloan = loanDAO.getLatestLoansByBorrower(bor.getBorrowerid());
                            latestloanver = lvd.getLatestloanver(bor.getBorrowerid());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String latestloaninfo = "";
                        String latestloanverinfo = "";
                        if (latestloan != null && latestloanver != null) {
                            String loanamount = Double.toString(latestloan.getLoanamount());
                            String term = Integer.toString(latestloan.getTerm());
                            String balance = Double.toString(latestloan.getBalance());
                            String releasedate = latestloan.getReleasedate();
                            String loanno = Integer.toString(latestloan.getLoanno());
                            String daysleft = Integer.toString(latestloan.getDaysleft());
                            String accountno = "";

                            latestloaninfo = "loan:" + loanamount + "," + term + "," + balance + "," + releasedate + "," + loanno + "," + daysleft + "," + accountno + "," + Integer.toString(latestloan.getLoanid()) + "|";

                            String loantype = latestloanver.getLoantype();
                            String loanclass = latestloanver.getProducts().getProductdesc();

                            latestloanverinfo = "loanver:" + loantype + "," + loanclass;
                        }

                        String nameandadd = borrowercode + name + address + areacode + latestloaninfo + latestloanverinfo;
//                        System.out.println("NAMEANDADD:" + nameandadd);
                        inputStream = new StringBufferInputStream(nameandadd);
                        ServletActionContext.getRequest().setAttribute("borrowerid", latestloan.getBorrowers().getBorrowerid().toString());

                        hasMatch = true;
                    }

                    if (hasMatch == true) {
                        return SUCCESS;
                    }
                }

                if (hasMatch == false) {
                    inputStream = new StringBufferInputStream("exist");
                    return SUCCESS;
                }
            } else if (!firstname.isEmpty() && !middlename.isEmpty() && !lastname.isEmpty()) {
                allbors = bo.getAll();
                for (int i = 0; i < allbors.size(); i++) {
                    bor = allbors.get(i);
                    String borcode = allbors.get(i).getBorrowercode();
                    if (firstname.equals(bor.getFirstname()) && middlename.equals(bor.getMiddlename()) && lastname.equals(bor.getLastname())) {
                        String borrowercode = "code:" + bor.getBorrowercode() + "," + bor.getBorrowerid() +"|";
                        String name = "name:" + bor.getFirstname() + "," + bor.getMiddlename() + "," + bor.getLastname() + "|";
                        String address = "address:" + bor.getStreetaddress() + "," + bor.getBarangay() + "," + bor.getCityormunicipality() + "," + bor.getProvince() + "|";

                        String areacode = "area:" + bor.getAreacode() + "|";
                        Loans latestloan = null;
                        Loanverification latestloanver = null;
                        try {
                            latestloan = loanDAO.getLatestLoansByBorrower(bor.getBorrowerid());
                            latestloanver = lvd.getLatestloanver(bor.getBorrowerid());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String latestloaninfo = "";
                        String latestloanverinfo = "";
                        if (latestloan != null && latestloanver != null) {
                            String loanamount = Double.toString(latestloan.getLoanamount());
                            String term = Integer.toString(latestloan.getTerm());
                            String balance = Double.toString(latestloan.getBalance());
                            String releasedate = latestloan.getReleasedate();
                            String loanno = Integer.toString(latestloan.getLoanno());
                            String daysleft = Integer.toString(latestloan.getDaysleft());
                            String accountno = "";

                            latestloaninfo = "loan:" + loanamount + "," + term + "," + balance + "," + releasedate + "," + loanno + "," + daysleft + "," + accountno + "," + Integer.toString(latestloan.getLoanid()) + "|";

                            String loantype = latestloanver.getLoantype();
                            String loanclass = latestloanver.getProducts().getProductdesc();

                            latestloanverinfo = "loanver:" + loantype + "," + loanclass;
                        }

                        String nameandadd = borrowercode + name + address + areacode + latestloaninfo + latestloanverinfo;
                        inputStream = new StringBufferInputStream(nameandadd);
//                        ServletActionContext.getRequest().setAttribute("borrowerid", latestloan.getBorrowers().getBorrowerid().toString());
                        hasMatch = true;
                    }

                    if (hasMatch == true) {
                        return SUCCESS;
                    }
                }

                if (hasMatch == false) {
                    inputStream = new StringBufferInputStream("exist");
                    return SUCCESS;
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
}
